import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_demo2/base/plugin/common_native.dart';
import 'package:flutter_demo2/leak/leak_watcher/src/widgets/all_leak_page.dart';
import 'package:flutter_demo2/leak/leak_watcher/src/widgets/leak_observer.dart';
import 'package:flutter_demo2/leak/leak_watcher_test/page1.dart';
import 'package:flutter_demo2/leak/leak_watcher_test/page2.dart';
import 'package:flutter_demo2/leak/leak_watcher_test/watch_object.dart';
import 'package:path_provider/path_provider.dart';

import 'async_page.dart';
import 'closure_page.dart';
import 'const_page.dart';

final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();

main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  void initState() {
    commonNative.finishSplash();
    super.initState();
  }

  Future<String> _getLeakSavePath() async {
    Directory appDocDir = await getApplicationDocumentsDirectory();
    String appDocPath = appDocDir.path;
    return appDocPath + "/leakFile.txt";
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      navigatorKey: navigatorKey,
      navigatorObservers: [
        LeakObserver(
          navigatorKey: navigatorKey,
          checkLeakDelay: 3,
          localSaveFilePath: _getLeakSavePath,
        )
      ],
      routes: <String, WidgetBuilder>{
        "page1": (BuildContext context) {
          return Page1();
        },
        "page2": (BuildContext context) {
          return Page2();
        },
        "closure": (BuildContext context) {
          return const ClosurePage();
        },
        "const": (BuildContext context) {
          return const ConstPage();
        },
        "async": (BuildContext context) {
          return AsyncPage();
        },
        'WatchObject': (_) {
          return WatchObjectPage();
        },
        'AllLeakPage': (_) {
          return const AllLeakPage();
        }
      },
      title: 'Expand',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      initialRoute: 'page1',
    );
  }
}
